﻿using System.Collections.Generic;
using System.Web;
using Sissonix.ExtRest.Exception;
using Sissonix.ExtRest.RestManagerModel.Model;
using Sissonix.ExtRest.RestManagerModel.Repository;
using Sissonix.Infrastructure.RepositoryFramework;
using Sissonix.ExtRest.WADL;

namespace Sissonix.ExtRest.RestManager.Processors
{
    class UserProcessor : BaseRestProcessor
    {
        IUserRepository repo;

        public UserProcessor() {
            try
            {
                repo = RepositoryFactory.GetRepository<IUserRepository, User>();
            }
            catch (System.Exception e)
            {
                throw new RestException("UserProcessor could not find a repository defined for User.", e);
            }
        }

        public new void ProcessRequest(WADLContext context)
        {
            base.ProcessRequest(context);
        }

        public override void ProcessDelete(WADLContext context)
        {
            //HttpRequest req = HttpContext.Current.Request;
            //int? id = context.DomainId;

            //int x;
            //if (id == 0 && int.TryParse(req.Form["UserID"],out x))
            //{
            //    id = int.Parse(req.Form["UserID"]);
            //}
            //else
            //{
            //    string ids = req.Form["ids"];
            //    string frm = req.Form.ToString();
            //    frm = HttpUtility.HtmlDecode(frm);

            //    IUserRepository repo = RepositoryFactory.GetRepository<IUserRepository, User>();
            //    repo.Remove(long.Parse(ids));
            //}
        }

        /*Get is for Retrievals*/
        public override void ProcessGet(WADLContext context)
        {
            //HttpContext ctx = HttpContext.Current;
            //if (context.DomainId.HasValue)
            //{

            //    User u = repo.FindBy(context.DomainId.Value);
            //    ctx.Response.Write(RestHelper.FormatObjectResponse<User>(u,context.ResponseFormat));
            //}
            //else
            //{
            //    IList<User> records = repo.FindAll(context.Start, context.Limit, context.Sort, context.SortDirection);
            //    int cnt = repo.Count();
            //    ctx.Response.Write(RestHelper.FormatListResponse(records, context.ResponseFormat, cnt));
            //}
        }

        /*Post is for Adds*/
        public override void ProcessPost(WADLContext context)
        {
            //ResponseFormat fmt = ResponseFormat.EXTJS_JSON; //the requested response format.

            //if (!context.DomainId.HasValue)
            //{
            //    string errmsg = RestHelper.FormatErrorResponse("A numeric UserID was not found in either the URL or the posted form.");
            //    HttpContext.Current.Response.Write(errmsg);
            //    return;
            //}
            //HttpContext ctx = HttpContext.Current;
            //User rec = (User)ProcessorHelper.GetDeserializedObjectFromRequestStream(ctx, typeof(User));
            //IUserRepository repo = RepositoryFactory.GetRepository<IUserRepository, User>();
            //repo.Change(rec);
            //ctx.Response.Write(RestHelper.FormatObjectResponse<User>(rec, fmt));
        }
        
        /*Put is for Updates*/
        public override void ProcessPut(WADLContext context)
        {
            //ResponseFormat fmt = ResponseFormat.EXTJS_JSON; //the requested response format.
            //HttpContext ctx = HttpContext.Current;
            //User rec = (User)ProcessorHelper.GetDeserializedObjectFromRequestStream(ctx, typeof(User));

            //IUserRepository repo = RepositoryFactory.GetRepository<IUserRepository, User>();
            //repo.Add(rec);
            //ctx.Response.Write(RestHelper.FormatObjectResponse<User>(rec, fmt));
        }
    }
}